//
//  MahuaViewController.m
//  KL
//
//  Created by mac on 15/12/14.
//  Copyright © 2015年 zhouxubin. All rights reserved.
//
#define KLChannelCount 4
#define KLNavTitleViewW 250.0

#import "MahuaViewController.h"
#import "JokesViewController.h"
#import "KLMahuaCollectionViewCell.h"
#import "KLNavTitleView.h"

@interface MahuaViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *channelView;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowlayout;
@property (nonatomic, weak) UILabel *imageLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *channelLabelTop;
/** 标记label是否正在动画 */
@property (nonatomic, assign, getter=isAnimating) BOOL animating;
@property (nonatomic, weak) KLNavTitleView *navTitleView;

@end

@implementation MahuaViewController

#pragma mark - 设置布局
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    // 设置item的大小
    self.flowlayout.itemSize = CGSizeMake(KLScreenWidth, KLScreenHeight - 64);
    // 设置行间距
    self.flowlayout.minimumInteritemSpacing = 0;
    self.flowlayout.minimumLineSpacing = 0;
    // 设置滚动方式
    self.flowlayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    // 设置分页效果
    self.collectionView.pagingEnabled = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.collectionView.backgroundColor = [UIColor whiteColor];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 添加顶部不同模块的label
    [self addChannelLabel];
    
    // 设置titleview
    [self setUpTitleView];
    
    // 添加通知观察者
    [KLNotificationCenter addObserver:self selector:@selector(beginDragging) name:@"beginDragging" object:nil];
    [KLNotificationCenter addObserver:self selector:@selector(endDragging) name:@"endDragging" object:nil];
    [KLNotificationCenter addObserver:self selector:@selector(showChannelLabel) name:@"didLoadData" object:nil];
}

#pragma mark - 设置titleview
- (void)setUpTitleView {
    KLNavTitleView *titleView = [KLNavTitleView navTitleView];
    self.navTitleView = titleView;
    self.navigationItem.titleView = titleView;
}

#pragma mark - 刷新item的点击事件
- (IBAction)getNewData:(UIBarButtonItem *)sender {
    [KLNotificationCenter postNotificationName:@"getNewData" object:nil];
}

#pragma mark - 通知方法
- (void)showChannelLabel {
    if (self.isAnimating) return;
    [UIView animateWithDuration:0.25 delay:1.0 options:0 animations:^{
        self.channelView.y = 34;
        self.animating = YES;
    } completion:^(BOOL finished) {
        self.animating = NO;
    }];
}

// 让顶部模块label显示
- (void)beginDragging {
    // 如果正在动画,直接返回
    if (self.isAnimating) return;
    [UIView animateWithDuration:0.25 animations:^{
        self.channelView.y = 64;
        self.animating = YES;
    } completion:^(BOOL finished) {
        self.animating = NO;
    }];
}

// 让顶部模块label消失
- (void)endDragging {
    // 如果正在动画,直接返回
    if (self.isAnimating) return;
    [UIView animateWithDuration:0.25 delay:1.5 options:0 animations:^{
        self.channelView.y = 34;
        self.animating = YES;
    } completion:^(BOOL finished) {
        self.animating = NO;
    }];
}

- (void)dealloc {
    [KLNotificationCenter removeObserver:self];
}

#pragma mark - 添加顶部不同模块的label
- (void)addChannelLabel {
    
    [self createChannelLabel:@"随便逛逛"];
    [self createChannelLabel:@"精华"];
    [self createChannelLabel:@"有图有真相"];
    [self createChannelLabel:@"纯文"];

    // 设置frame
    [self setUpChannelLabelFrame];
    
    // 添加标签
    self.imageLabel.x = (KLScreenWidth / KLChannelCount - self.imageLabel.w) * 0.5;
    self.imageLabel.y = self.channelView.h - self.imageLabel.h;
}

- (void)createChannelLabel:(NSString *)title {
    UILabel *label = [[UILabel alloc] init];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:11];
    label.textColor = [UIColor darkGrayColor];
    label.text = title;
    [self.channelView addSubview:label];
}

// 设置label的frame
- (void)setUpChannelLabelFrame {
    CGFloat labelW = KLScreenWidth / KLChannelCount;
    CGFloat labelH = self.channelView.h;
    // 遍历channellabel
    for (int i = 0;i < self.channelView.subviews.count; i++) {
        UILabel *label = self.channelView.subviews[i];
        label.frame = CGRectMake(i * labelW, 0, labelW, labelH);
    }
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return KLChannelCount;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"home_cell";
    KLMahuaCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    cell.index = indexPath.row;
    KLLog(@"%ld", indexPath.item);
    // 根据不同的cell,请求不同的数据
    if (indexPath.row == 0) {
        cell.urlString = api_stroll_latest;
    }else if (indexPath.row == 1) {
        cell.urlString = api_elite_day;
    }else if (indexPath.row == 2) {
        cell.urlString = api_imagetruth_images;
    }else if (indexPath.row == 3) {
        cell.urlString = api_pure_text;
    }
    
    return cell;
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    // 只要一滚动就调用这个方法
    CGFloat offset = scrollView.contentOffset.x / KLScreenWidth;
    
    // 算出titleview的偏移量,用scrollview得偏移量乘上一个比例,就可以得到titleview的偏移量
    CGFloat titleViewOffsetX = scrollView.contentOffset.x * (KLNavTitleViewW / KLScreenWidth);
    self.navTitleView.offsetX = titleViewOffsetX;
    
    
    // 改变黄色小iamgeview得位移
    self.imageLabel.transform = CGAffineTransformMakeTranslation((offset * (KLScreenWidth / KLChannelCount)), 0);
    
    // 显示channellabel, 如果正在动画,直接返回
    if (self.isAnimating) return;
    [UIView animateWithDuration:0.25 animations:^{
        self.channelView.y = 64;
        self.animating = YES;
    } completion:^(BOOL finished) {
        self.animating = NO;
    }];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGFloat offset = scrollView.contentOffset.x / KLScreenWidth;
    // 取出四舍五入之后的label
    NSInteger index = (NSInteger)(offset + 0.5);
    KLLog(@"%ld", index);
    UILabel *label = self.channelView.subviews[index];
    self.title = label.text;
    
    // 取出滚动结束的当前的label
    for (int i = 0; i < self.channelView.subviews.count; i++) {
        UILabel *subLabel = self.channelView.subviews[i];
        if (i == index) {
            subLabel.textColor = [UIColor blackColor];
        }else {
            subLabel.textColor = [UIColor darkGrayColor];
        }
    }
    
    // 如果正在动画就return, 如果正在动画直接返回
    if (self.isAnimating) return;
    [UIView animateWithDuration:0.25 delay:2.0 options:0 animations:^{
        self.channelView.y = 34;
        self.animating = YES;
    } completion:^(BOOL finished) {
        self.animating = NO;
    }];
    
}

#pragma mark - 懒加载
- (UILabel *)imageLabel {
    if (!_imageLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.backgroundColor = [UIColor orangeColor];
        label.w = 40;
        label.h = 5;
        [self.channelView addSubview:label];
        _imageLabel = label;
    }
    return _imageLabel;
}

@end
